TokensV2Controller.consume   B
last analyzed

Complexity

Conditions 1

Size

Total Lines 42
Code Lines 37

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 37
dl 0
loc 42
ccs 5
cts 5
cp 1
crap 1
rs 8.9919
c 0
b 0
f 0
1 8
import { Controller, Post, Param } from '@nestjs/common';
2 8
import { TokensService } from './tokens.service';
3 8
import { TokenResponseDto } from './dto/token-response.dto/token-response.dto';
4 8
import { ApiOperation, ApiResponse, ApiTags } from '@nestjs/swagger';
5
6
@ApiTags('Tokens')
7
@Controller({ path: 'tokens', version: '2' }) // Version 2
8 8
export class TokensV2Controller {
9 6
  constructor(private readonly tokensService: TokensService) {}
10
11
  @Post(':id/consume')
12
  @ApiOperation({
13
    summary: 'Consume a token (v2)',
14
    description: 'Consumes a token and provides an insight quote from a single person.',
15
  })
16
  @ApiResponse({
17
    status: 201,
18
    description: 'Token consumed successfully',
19
    type: TokenResponseDto,
20
  })
21
  @ApiResponse({
22
    status: 400,
23
    description: 'Bad Request - Token not found or already expired',
24
  })
25
  @ApiResponse({
26
    status: 403,
27
    description: 'Forbidden - Token has no remaining uses',
28
  })
29 8
  async consume(@Param('id') id: string): Promise<TokenResponseDto> {
30 1
    const token = await this.tokensService.consume(id);
31
32 1
    const rooseveltQuotes = [
33
      "Believe you can and you're halfway there.",
34
      'Do what you can, with what you have, where you are.',
35
      'It is hard to fail, but it is worse never to have tried to succeed.',
36
      'Keep your eyes on the stars, and your feet on the ground.',
37
      'In any moment of decision, the best thing you can do is the right thing.',
38
      'The only man who never makes mistakes is the man who never does anything.',
39
      'Courage is not having the strength to go on; it is going on when you don’t have the strength.',
40
      'Far better it is to dare mighty things than to rank with those timid souls who neither enjoy much nor suffer much.',
41
      'People don’t care how much you know until they know how much you care.',
42
      'Do what you can, with what you’ve got, where you are.',
43
    ];
44
45 1
    const randomQuote = rooseveltQuotes[Math.floor(Math.random() * rooseveltQuotes.length)];
46
47 1
    return {
48
      token: token.id,
49
      remainingUses: token.remainingUses,
50
      expiresAt: token.expiresAt,
51
      message: `This is a v2 response: Here's a quote from Theodore Roosevelt: "${randomQuote}"`,
52
    };
53
  }
54
}
55